package com.strange.mdl.module.mdl.controller.admin.user;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.strange.mdl.framework.common.pojo.PageParam;
import com.strange.mdl.framework.common.pojo.PageResult;
import com.strange.mdl.framework.common.pojo.CommonResult;
import com.strange.mdl.framework.common.util.object.BeanUtils;
import static com.strange.mdl.framework.common.pojo.CommonResult.success;

import com.strange.mdl.framework.excel.core.util.ExcelUtils;

import com.strange.mdl.framework.apilog.core.annotation.ApiAccessLog;
import static com.strange.mdl.framework.apilog.core.enums.OperateTypeEnum.*;

import com.strange.mdl.module.mdl.controller.admin.user.vo.*;
import com.strange.mdl.module.mdl.dal.dataobject.user.MdlUserDO;
import com.strange.mdl.module.mdl.service.user.MdlUserService;

@Tag(name = "管理后台 - 用户")
@RestController
@RequestMapping("/mdl/user")
@Validated
public class MdlUserController {

    @Resource
    private MdlUserService userService;

    @PostMapping("/create")
    @Operation(summary = "创建用户")
    @PreAuthorize("@ss.hasPermission('mdl:user:create')")
    public CommonResult<Long> createUser(@Valid @RequestBody MdlUserSaveReqVO createReqVO) {
        return success(userService.createUser(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户")
    @PreAuthorize("@ss.hasPermission('mdl:user:update')")
    public CommonResult<Boolean> updateUser(@Valid @RequestBody MdlUserSaveReqVO updateReqVO) {
        userService.updateUser(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mdl:user:delete')")
    public CommonResult<Boolean> deleteUser(@RequestParam("id") Long id) {
        userService.deleteUser(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除用户")
                @PreAuthorize("@ss.hasPermission('mdl:user:delete')")
    public CommonResult<Boolean> deleteUserList(@RequestParam("ids") List<Long> ids) {
        userService.deleteUserListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mdl:user:query')")
    public CommonResult<MdlUserRespVO> getUser(@RequestParam("id") Long id) {
        MdlUserDO user = userService.getUser(id);
        return success(BeanUtils.toBean(user, MdlUserRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户分页")
    @PreAuthorize("@ss.hasPermission('mdl:user:query')")
    public CommonResult<PageResult<MdlUserRespVO>> getUserPage(@Valid MdlUserPageReqVO pageReqVO) {
        PageResult<MdlUserDO> pageResult = userService.getUserPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MdlUserRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户 Excel")
    @PreAuthorize("@ss.hasPermission('mdl:user:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportUserExcel(@Valid MdlUserPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MdlUserDO> list = userService.getUserPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "用户.xls", "数据", MdlUserRespVO.class,
                        BeanUtils.toBean(list, MdlUserRespVO.class));
    }

}